package net.homelinux.chaoswg.io.renga.krypto.animations.Shift;

import net.homelinux.chaoswg.io.renga.core.animations.Animation;
import net.homelinux.chaoswg.io.renga.core.animations.State;
import net.homelinux.chaoswg.io.renga.krypto.animations.Shift.ShiftState;
import net.homelinux.chaoswg.io.renga.krypto.animations.Shift.ShiftStateChanger;

public class ShiftAnimation extends Animation {
    ShiftState initialState;
    ShiftStateChanger ShiftChanger;
    
    public static final int LEFT = 0;
    public static final int RIGHT = 1;

    public ShiftAnimation (int direction) {
        initialize(new ShiftState (), direction);
    }

    public ShiftAnimation (ShiftState initialState, int direction) {
        initialize(initialState, direction);
    }

    private void initialize(State initialState, int direction) throws IllegalArgumentException {
        if (!(initialState instanceof ShiftState)) {
            throw new IllegalArgumentException ();
        }
        this.initialState = ((ShiftState)initialState);
        this.ShiftChanger = new ShiftStateChanger (this.initialState, direction);
    }

    @Override
    public synchronized void start() {
        Thread t = new Thread (ShiftChanger);
        t.setDaemon(true);
        t.start();
        
        try {
            t.join();
        } catch (InterruptedException iexcp){ 
            iexcp.printStackTrace();
        }
        ShiftChanger.close ();
        finished = true;
        setChanged();
        notifyObservers();
    }

    @Override
    public void setOperand(int i, Object operand) {
        initialState.setOperand(i, operand);
    }
}
